Page({
    /**
     * 页面的初始数据
     */
    data: {
        ctx: {},
        position: "back",
        iknow: true,
        temPhoto: "",
        agressTxt: "[ 我们将保障您的个人信息安全,不会泄露您的拍摄照片 ]",
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        wx.getSystemInfo({
            success: res => {
                // console.log(res)
                this.setData({
                    CaHeight: res.screenHeight - 105 + "px",
                    statusBarHeight: res.statusBarHeight
                })
            },
        })
        let ctx = wx.createCameraContext()
        this.setData({
            ctx: ctx
        })
        let openid = wx.getStorageSync('ACCESS_TOKEN')
        let nickname = wx.getStorageSync('appUserInfo').nickName
        let avatar = wx.getStorageSync('appUserInfo').avatarUrl
        let unionid = wx.getStorageSync('appUserInfo').union_id
        let base64String = ""

    },
    cancelNotice() {
        this.setData({
            iknow: false
        })
    },
    naviback() {
        wx.navigateBack({
            delta: 1,
        })
    },
    jumpMXSW() {
        wx.navigateTo({
            url: '../mxsw/index',
        })
    },
    exchangeJT() {
        // console.log("12")
        let position = this.data.position
        this.setData({
            position: position == 'back' ? 'front' : 'back'
        })
    },
    // 预览
    previewImg() {
        wx.previewImage({
            urls: [this.data.temPhoto],
        })
    },
    // 人脸检查
    faceCheck(data){
        wx.request({
            url: getApp().pfjc,
            method: "POST",
            data: data,
            header: {
                'content-type': 'application/json'
            },
            success: (result => {
                // console.log("res", result)
                wx.hideLoading()
                if (result.data.code == '0') {
                   
                    // this.jumpMXSW()
                    wx.previewImage({
                      urls: [result.data.imgurl],
                    })
                }else{
                    let msg =result.data.msg
                    wx.showToast({title: msg,icon: 'none',duration: 2000})
                }
            }),
            fail: (fail => {
                wx.hideLoading()
                // console.log("err", fail)
            })
        })
    },
    // 打开相册
    openXC(){
        wx.chooseImage({
          count: 1,
          sourceType:["album"],
          sizeType:['original'],
          success:res1=>{
            wx.showLoading({
                title: '处理中,请稍等！',
            })
            this.setData({
                temPhoto:res1.tempFilePaths[0]
            })
            // console.log(res1)
            let url = res1.tempFilePaths[0]
            
            wx.getFileSystemManager().readFile({
                filePath: url, //选择图片返回的相对路径
                encoding: 'base64', //编码格式
                success: res => { //成功的回调
                    // console.log('data:image/png;base64,' + res.data)
                    this.setData({
                        src: 'data:image/png;base64,' + res.data
                    })
                    let data = {
                        openid: wx.getStorageSync('ACCESS_TOKEN'),
                        nickname: wx.getStorageSync('appUserInfo').nickName,
                        avatar: wx.getStorageSync('appUserInfo').avatarUrl,
                        unionid: wx.getStorageSync('appUserInfo').union_id,
                        base64String: res.data
                    }
                    this.faceCheck(data)   
                }
            })


          }
          
        })
    },
    takePhoto() {
        if (this.data.iknow) {
            this.cancelNotice()
            return false
        }
        let ctx = this.data.ctx
        ctx.takePhoto({
            quality: 'high',
            success: (res) => {
                let url = res.tempImagePath;
                this.setData({
                    temPhoto: res.tempImagePath
                })
                wx.showLoading({
                    title: '处理中,请稍等！',
                })
                wx.getFileSystemManager().readFile({
                    filePath: url, //选择图片返回的相对路径
                    encoding: 'base64', //编码格式
                    success: res => { //成功的回调
                        // console.log('data:image/png;base64,' + res.data)
                        this.setData({
                            src: 'data:image/png;base64,' + res.data
                        })
                        let data = {
                            openid: wx.getStorageSync('ACCESS_TOKEN'),
                            nickname: wx.getStorageSync('appUserInfo').nickName,
                            avatar: wx.getStorageSync('appUserInfo').avatarUrl,
                            unionid: wx.getStorageSync('appUserInfo').union_id,
                            base64String: res.data
                        }
                        this.faceCheck(data)   
                    }
                })
            }
        })
    }
})